﻿//#ifdef _WIN32
//#include <vld.h>
//#endif
#include "mysql/prepare/ssMySQLConnection.h"
#include "mysql/prepare/ssMySQLStmtQuery.h"
//#include "mysql/prepare/xml/ssXmlDBGroup.h"
#include "log/ssLogThread.h"
#include "byteStream/ssReadBytes.h"
#include "time/ssTime.h"
#include <iostream>
#include "mysql/prepare/xml/ssXmlDBThread.h"
#include "net/ssNetBase.h"
#include "libevent/ssLeServerThread.h"
#include "actor/ssActorMgr.h"
#include "ssRedisSql.h"


using namespace StarSeeker;


int main()
{
	LoadSocketLib();
	SS_LOG->StartThread();

	{//数据库预处理查询
		//ssMySQLConnection mysqlConnection;
		//mysqlConnection.Init("127.0.0.1", 3306, "root", "123456", "scratch-server");
		//mysqlConnection.Execute("SELECT * FROM tb_account LIMIT 10;");
		//mysqlConnection.Execute("SELECT * FROM tb_account LIMIT 10;");
		//mysqlConnection.Execute("SELECT * FROM tb_account LIMIT 10;");

		ssMySQLStmtQuery mySQLStmtQuery;
		mySQLStmtQuery.Init("127.0.0.1", 3306, "root", "123456", "xia");
		mySQLStmtQuery.Execute("SELECT * FROM tb_role LIMIT 10;");
		mySQLStmtQuery.StoreResult();
		mySQLStmtQuery.ClearResult();
		mySQLStmtQuery.Execute("SELECT * FROM tb_role LIMIT 10;");
		mySQLStmtQuery.StoreResult();
		mySQLStmtQuery.ClearResult();
		mySQLStmtQuery.Execute("SELECT * FROM tb_role LIMIT 10;");
		mySQLStmtQuery.StoreResult();
		mySQLStmtQuery.ClearResult();

		mySQLStmtQuery.Query("SELECT rid,uid,role_name,gender,sect,lv,is_delete,create_time FROM tb_role LIMIT 10;", 0, "i8,i8,s64,i1,i1,i4,dt,dt", DB_RESULT_MULTI, 0);
		std::pair<int, ssLineBuffer&> ret = mySQLStmtQuery.GetResultSet();
		ssLineBuffer::Iterator itr = ret.second.Begin();
		while (itr.IsVaild())
		{
			std::string temp;
			ssYMDHMS time;
			char timeStr[32];

			ssReadBytes rb(itr.Data(), itr.Size());

			std::cout << *rb.ReadU64() << " ";
			std::cout << *rb.ReadU64() << " ";

			rb.ReadString(temp);
			std::cout << temp << " ";

			std::cout << (int)*rb.ReadChar() << " ";
			std::cout << (int)*rb.ReadChar() << " ";

			std::cout << *rb.ReadI32() << " ";

			rb.ReadData(&time, sizeof(ssYMDHMS)); time.GetLocalTime(timeStr);
			std::cout << timeStr << " ";

			rb.ReadData(&time, sizeof(ssYMDHMS)); time.GetLocalTime(timeStr);
			std::cout << timeStr << " " << std::endl;

			ret.second.Next(itr);
		}

		mySQLStmtQuery.Transaction();
		mySQLStmtQuery.Query("DELETE FROM tb_account WHERE user_id=?;", "u4", 0, 0, 1, 18);
		mySQLStmtQuery.Commit();
	}

	//{//数据库消息线程
	//	class XmlDBThreadTest : public StarSeeker::ssXmlDBThread
	//	{
	//	protected:
	//		//发送查询回包
	//		void SendResponse(u32 connectID, ssNetMsgHead* pHead, char* pData, u32 len)
	//		{
	//			ssMultiLineBuffer buffer;
	//			buffer.FormatIn(pData + MSG_HEADER_LEN + sizeof(ssDBResParam), len - MSG_HEADER_LEN - sizeof(ssDBResParam));
	//			ssMultiLineBuffer::Iterator itr = buffer.Begin();
	//			std::cout << "=====================================" << std::endl;
	//			while (itr.IsVaild())
	//			{
	//				std::string temp;
	//				ssYMDHMS time;
	//				char timeStr[32];

	//				ssReadBytes rb(itr.Data(), itr.Size());

	//				std::cout << *rb.ReadU32() << " ";

	//				rb.ReadString(temp);
	//				std::cout << temp << " ";

	//				rb.ReadData(&time, sizeof(ssYMDHMS)); time.GetLocalTime(timeStr);
	//				std::cout << timeStr << " ";

	//				std::cout << *rb.ReadU32() << " ";

	//				rb.ReadData(&time, sizeof(ssYMDHMS)); time.GetLocalTime(timeStr);
	//				std::cout << timeStr << " ";

	//				rb.ReadString(temp);
	//				std::cout << temp << " ";

	//				std::cout << *rb.ReadUChar() << " ";

	//				rb.ReadString(temp);
	//				std::cout << temp << std::endl;

	//				buffer.Next(itr);
	//			}
	//			delete[] pData;
	//		}
	//	};

	//	ssXmlDBGroup<XmlDBThreadTest> xmlDBThreadTest;
	//	xmlDBThreadTest.Init("127.0.0.1", 3306, "root", "123456", "scratch-server", 5, 2);
	//	xmlDBThreadTest.ReadAllCfg("./dbcfg.xml");
	//	xmlDBThreadTest.Start();

	//	char* buf = new char[sizeof(ssNetMsgHead)];
	//	ssNetMsgHead* pNetMsg = (ssNetMsgHead*)buf;
	//	pNetMsg->m_dwMsgID = 101;
	//	pNetMsg->m_dwLen = sizeof(ssNetMsgHead);
	//	xmlDBThreadTest.PostMsg(1000, 0, 0, (ulint)buf, 0);

	//	ssThreadBase::Sleep(3000);
	//}

	//{//libevent核心
	//	bool ok = true;
	//	ssLeServerThread leServer;
	//	leServer.StartNetWork("127.0.0.1", 7001, false, 0);
	//	u32 begin = ssTime::GetTimeNow().u32p;
	//	while (ok)
	//	{
	//		ssThreadBase::Sleep(100);
	//		u32 now = ssTime::GetTimeNow().u32p;
	//		if (now - begin > 15)
	//		{
	//			ok = false;
	//		}
	//	}
	//}

	//{//actor
	//	class TestActor : public ssActor
	//	{
	//	private:
	//		void HandleMsg(ssMsg_s &m)
	//		{
	//			std::cout << "actor num: " << m.param[2] << std::endl;
	//		}
	//	};

	//	ssActorMgr mgr;
	//	mgr.AddActor(new TestActor());
	//	mgr.AddActor(new TestActor());
	//	mgr.CreatWorkThreads(2);
	//	mgr.Start();
	//	for (u32 i=0; i<100; ++i)
	//	{
	//		mgr.PostMsg(true, 1, 10, 10, 0, 0, i);
	//		if (i==50)
	//		{
	//			mgr.DiscardActor(1);
	//		}
	//		mgr.PostMsg(true, 2, 10, 10, 0, 0, i+100);
	//	}
	//	ssThreadBase::Sleep(3000);
	//	mgr.DeleteActor();
	//}

	//{
	//	ssRedisConnection redis("192.168.0.254", 6379);
	//	std::string out;
	//	std::list<std::pair<std::string, std::string> > out2;
	//	int ret = redis.Ping();
	//	ret = redis.TTL("foo");
	//	ret = redis.Set("test1", "11223355", 120);
	//	ret = redis.Get("test1", out);
	//	ret = redis.Del("test1");
	//	ret = redis.Mset("11", "22", "33", "44", "55", "66");
	//	ret = redis.Hset("test2", "1212", 5556);
	//	ret = redis.Hget("test2", "1212", out);
	//	ret = redis.HMset("test2", "1213", 48891, "1214", "sdag", "1215", 6415561);
	//	ret = redis.Hdel("test2", "1212", "1213");
	//	ret = redis.HgetAll(out2, "test2");
	//}

	//{//redissql
	//	ssRedisSql rs;
	//	rs.Init("192.168.0.254", "xia", "y6MwwjMTyFmmRChB", "xia", 3306, "192.168.0.254", 6379);
	//	//rs.Tables("tb_test").Fields({ "rid","type","val1" ,"val2" ,"val3" }).Values({ 99998666666, 33, "1234", 1234, 12.34f }).Keys({ "rid",99998666666,"type", 33}).Insert();
	//	//rs.Tables("tb_test").Fields({ "val1" ,"val2" ,"val3" }).Values({ "12345", 12345, 12.345f }).Keys({ "rid",99998666666,"type", 33 }).Update();
	//	std::vector<std::string> out;
	//	rs.Tables("tb_test11").Fields({ "val1" ,"val2" ,"val3" }).Keys({ "rid",99998666666,"type", 33 }).Select(out);
	//}

	StarSeeker::ssSingle<StarSeeker::ssLogThread>::delete_instance();
	ssCallStack::delete_instance();

	UnloadSocketLib();
	return 0;
}
